<?php

namespace App\Shop\Controllers;

use App\Admin\Forms\Service\HandleCash;
use App\Admin\Repositories\CashOrder;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;

class CashOrderController extends AdminController
{

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        return Grid::make(new CashOrder(['shop','user']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableRowSelector();
            $admin = Admin::user();
            $isShop = $admin->isAdministrator();
            if ($isShop){
                $shop_id = $admin->id;
            }else{
                $shop_id = $admin->shop_id;
            }
            $grid->model()->where("type",0)->where("shop_id",$shop_id);
            $grid->column('out_trade_no');
            $grid->column('user_id');
            $grid->column('user.nickname','用户昵称');
            $grid->column('amount');
            $grid->column('banks.bank_name','开户行名称');
            $grid->column('banks.bank_num','银行卡号');
            $grid->column('banks.name','开户姓名');
            $options = ['待审核','审核通过','审核拒绝'];
            $labels = ['default','success','danger'];
            $grid->column('status')->if(function ($column) {
                return $column->getValue() == 0;
            })->using($options)->label($labels)->modal("审核",function (){
                return HandleCash::make()->payload(['oid'=>$this->id]);
            })->else()->using($options)->label($labels);
            $grid->column('created_at');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->equal('id');
                $filter->like('out_trade_no','提现单号');
                $filter->equal('user_id','用户ID');
                $filter->like('user.nickname','用户昵称');
                $filter->like('banks->bank_name','开户行名称');
                $filter->like('banks->bank_num','银行卡号');
                $filter->like('banks->name','开户姓名');
            });
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '审核状态', ['待审核','审核通过','审核拒绝']);
            });
        });
    }




    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CashOrder('user'), function (Form $form) {
            $form->display('id');
            $form->display('out_trade_no');
            $form->display('user_id');
            $form->display('user.nickname');
            $form->display('amount');
            $status = $form->model()->status;
            if (!$status){
                $form->radio("status","审核状态")->options([0=>'待审核',1=>"审核通过",2=>'审核拒绝']);
            }else{
                $form->display('status',"审核状态")->with(function ($value){
                    return $value==1?"审核通过":"审核拒绝";
                });
            }
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
